home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13941 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: news.ichange.com!newsmaster
  2. From: Jesse Liberty <jl@staff.ichange.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: when to inherit, when to compose...
  5. Date: Wed, 27 Mar 1996 13:41:17 -0500
  6. Organization: AT&T
  7. Message-ID: <31598BCD.6BF7@staff.ichange.com>
  8. References: <4j943p$7es@ulowell.uml.edu> <31587163.1045@datalytics.com>
  9. NNTP-Posting-Host: 140.244.99.60
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win95; I)
  14. CC: jl@staff.ichange.com
  15.  
  16. Rob Stewart wrote:
  17. > John Peter Lee wrote:
  18. >> .. Under what circumstances is it more
  19. > >   advantageous to use an inheritance hierarchy for a design, as opposed
  20. > >   to using object containment, and vice versa?
  21.  
  22. > ...inheritance applies only when the derived class is a form of the
  23. > base class, you inherit.  When it just happens to need to do
  24. > things another class offers, you use the other class. 
  25.  
  26. Well, yes and no. You actually have a few more options.  Let's say you have two classes foo and bar and are trying to decide 
  27. whether bar ought to inherit from foo.  The first question is, as you suggest, "is it true that a bar IS A foo?" If the 
  28. answer is yes, then you probably want bar to publicly inherit from foo.  
  29.  
  30. If foo has functionality you want to use, you can implement bar "in terms of" foo by using either private inheritance or 
  31. containment.  Private inheritance brings the ability to override methods but if you need more than one "instance" of a foo 
  32. then containment would be a better choice.
  33.  
  34. Obviously there is more to the decision, and even within the rubric of "containment" there are considerations abuot 
  35. delegation to be made.  I suggest a good book on object oriented design such as Martin's book on OO Analysis using C++.
  36.  
  37.  
  38. -j
  39.  
  40. ------
  41. Jesse Liberty [AT&T New Media Services]
  42. jl@staff.ichange.com    ZDNet: 72241,72
  43. Teach Yourself C++ In 21 Days. Sams 1994
  44. Teach Yourself MORE C++ In 21 Days. Sams 1996
  45. Teach Yourself ANSI C++ In 21 Days. Sams 1996
  46. C++: An Introduction To Programming. Que 1996
  47.